//
// Created by hujin on 2023/3/9.
//

#ifndef COSI_GLOBAL_STATS_STL_H
#define COSI_GLOBAL_STATS_STL_H

#include <vector>
#include <unordered_map>
#include <bits/unordered_set.h>
# include "global_stats.h"
#include <algorithm>


template <typename T, typename I> class GlobalStatsSTL : public GlobalStats<T, I>{

public:

    GlobalStatsSTL(const I s[], const I t[], const T weights[], I num_e, I id_max, T tol) : sum_in_tol(tol){
        A.resize(id_max + 1);
//        sum_in.resize(id_max + 1);
        removed_ids.reserve(id_max);
        remap_ids.reserve(id_max + 1);
        real_ids.reserve(id_max + 1);
        itoa(0, remap_ids.begin(), id_max);
        itoa(0, real_ids.begin(), id_max);

        for(int i = 0; i < num_e; i++){
            A[t[i]][s[i]] += weights[i];
//            sum_in[t[i]] += weights[i];
        }
    }

private:

    std::unordered_set<I > removed_ids;

    std::vector<std::unordered_map<I, T> &> A;

//    std::vector<T> sum_in;

    std::vector<I> remap_ids;
    std::vector<I> real_ids;

    T sum_in_tol;
public:

    T query(I es, I et) override ;

    void recycle(I node) override ;

    void combine(I id1, I id2, I id_new) override ;

    void copy(I id1, I id_new) override ;

    void remove(I id_s, I id_t) override ;

    void modify(I id_s, I id_t, T p) override;

    using FuncCallback = std::function<void(I)>;

    void iterate_neighbor(I id, std::function<void(I, T)>)  override;

    void query_sum_in(I id_t) override;

};



#endif //COSI_GLOBAL_STATS_STL_H
